package sit.engreal.leetcode.problems100;

import org.junit.jupiter.api.Assertions;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/remove-element/"> 27. 移除元素 </a>
 */
public class LeetCode27 {
    static class Solution {
        public int removeElement(int[] nums, int val) {
            int i = 0;
            int j = 0;
            while (j < nums.length) {
                if (nums[j] != val) {
                    nums[i] = nums[j];
                    i++;
                }
                j++;
            }
            return i;
        }
    }

    public static void main(String[] args) {
        final Solution solution = new Solution();
        {
            final int[] array = {0, 1, 2, 2, 3, 0, 4, 2};
            final int n = solution.removeElement(array, 2);

            final int[] expect = {0, 1, 4, 0, 3};
            final int[] output = Arrays.copyOf(array, n);
            Arrays.sort(output);
            Arrays.sort(expect);
            Assertions.assertArrayEquals(expect, output);
        }
    }
}
